home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / nihcl-30.lha / nihcl-3.0 / ex / ex11-1.c < prev    next >
C/C++ Source or Header  |  1990-05-15  |  2KB  |  74 lines

  1. // ex11-1.c -- Scheduling lightweight processes in
  2. //             the NIH class library
  3.  
  4. // $Header: /afs/alw.nih.gov/unix/sun4_40c/usr/local/src/nihcl-3.0/share/ex/RCS/ex11-1.c,v 3.0 90/05/15 22:44:19 kgorlen Rel $
  5.  
  6. #define BASE StackProc
  7. #if BASE == StackProc
  8. #include "StackProc.h"
  9. #endif
  10. #if BASE == HeapProc
  11. #include "HeapProc.h"
  12. #endif
  13.  
  14. #include "Scheduler.h"
  15. #include "Semaphore.h"
  16. #include "String.h"
  17.  
  18. class TestProcess : public BASE {
  19. public:
  20.     TestProcess(const char* name, stackTy* bot, int pri,
  21.                 Process* parent);
  22.     static TestProcess* create(const char* name, int pri,
  23.                                Process* parent);
  24. };
  25.  
  26. TestProcess::TestProcess(const char* pname,stackTy* bot,int pri,
  27.                          Process* parent)
  28.             : BASE(pname,bot,pri)
  29. {
  30.     // parent process yields to allow this process to start
  31.     if ( FORK() ) { Scheduler::yield(); return; }
  32.  
  33.     cout << name() << pri << " start" << endl;
  34.  
  35.     // yield to parent process
  36.     if (parent) {
  37.         parent->resume();
  38.         Scheduler::yield();
  39.         }
  40.     // suspend until child yields
  41.     suspend();
  42.     Scheduler::schedule();
  43.  
  44.     cout << name() << pri << " resume" << endl;
  45.  
  46.     // terminate to avoid return
  47.     terminate();
  48. }
  49.  
  50. TestProcess* TestProcess::create(const char* name, int pri,
  51.                                  Process* parent)
  52. {
  53.     // the next two statements must be in the same scope
  54.     // for the address of the stack bottom to be correct
  55.     auto stackTy bottom;
  56.     return new TestProcess(name,&bottom,pri,parent);
  57. }
  58.  
  59. main()
  60. {
  61.     // start Scheduler
  62.     // create main context with priority 0
  63.     MAIN_PROCESS(0);
  64.  
  65.     Process* parent =0;
  66.     String* pname = new String("P"); 
  67.     for (register int i=MAXPRIORITY; i>=1; i--)
  68.         parent = TestProcess::create(*pname,i,parent);
  69.  
  70.     cout << "main Process" << endl;
  71.     parent->resume();
  72.     Scheduler::yield();
  73. }
  74.